package BaseUserController

import (
	"github.com/gin-gonic/gin"
	"learn/app/controllers/cc"
	"learn/app/request"
	"learn/app/services/baseUserService"
)

type User struct {
	cc.BaseController
}

func (obj *User) Add(c *gin.Context) {
	userStruct := request.UserAdd{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct, "id"); ok == false {
		return
	}
	res, _ := new(baseUserService.User).Add(obj.ToMap(userStruct))
	cc.Return_with_code(res, c)
}

func (obj *User) Update(c *gin.Context) {
	userStruct := request.UserUpdate{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct); ok == false {
		return
	}
	res, _ := new(baseUserService.User).Update(obj.ToMap(userStruct))
	cc.Return_with_code(res, c)
}

func (obj *User) List(c *gin.Context) {
	userStruct := request.UserList{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct); ok == false {
		return
	}
	res, count := new(baseUserService.User).List(obj.ToMap(userStruct))
	cc.Return_success(c, cc.WithData(res), cc.WithCount(count))
}

func (obj *User) Del(c *gin.Context) {
	userStruct := request.UserDel{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct, "id"); ok == false {
		return
	}

	res := new(baseUserService.User).Del(obj.ToMap(userStruct))
	cc.Return_with_code(res, c)
}

func (obj *User) Login(c *gin.Context) {
	userStruct := request.UserLogin{}
	if ok := cc.BindParamAndCheckAndExpectField(c, &userStruct, "id"); ok == false {
		return
	}
	if bool, user := new(baseUserService.User).Login(obj.ToMap(userStruct)); bool == false {
		cc.Return_fail(c, cc.WithMsg("账号不存在"))
	} else {
		cc.Return_success(c, cc.WithData(user))
	}

}
